
var HomeLayer = cc.Layer.extend({
    ctor: function () {
        this._super();
    },
    onEnter: function () {
        this._super();   
        this.initView();
    },
    initView:function(){

    	let size = cc.winSize;
    	
    	//背景
    	let sprite = new cc.Sprite(res.homebg);
        this.addChild(sprite);
        sprite.x = size.width/2;
        sprite.y = size.height/2;

        sprite.scaleX = size.width/750;
        
        // 初始化按钮
        var menu = new cc.Menu();
        menu.x = 0;
        menu.y = 0;
        //把菜单按钮加到当前界面
        this.addChild(menu, 1);

        var startBtn = new cc.MenuItemImage(
					res.btnstart,
					res.btnstart,
					this.onStartBtn.bind(this),
					this);
        menu.addChild(startBtn);
        startBtn.x = size.width/2-200;
        startBtn.y = 100;

        var ruleBtn = new cc.MenuItemImage(
					res.btnrule,
					res.btnrule,
					this.onRuleBtn.bind(this),
					this);
        menu.addChild(ruleBtn);
        ruleBtn.x = size.width/2+200;
        ruleBtn.y = 100;
        if(cc.isHost){
            var editbtn = new cc.MenuItemFont("编辑关卡",this.onEdit,this);

            //editbtn.setColor(cc.color(255,0,0));
            //editbtn.getLabel().color = 
            editbtn.setFontSize(55);
            editbtn.x = cc.winSize.width/2;
            editbtn.y = cc.winSize.height/2+250;
            menu.addChild(editbtn);
            //console.log("****editbtn.getLabel()***",editbtn.getLabel());
        }

        // 狗子动画

        var rect = cc.rect(0, 0, 112, 225);
        var movingFrames = [];

        let anis =[res.ani1,res.ani2,res.ani3];
            var tex = cc.textureCache.addImage(anis[0]);
            let frame = new cc.SpriteFrame(tex,rect);
            movingFrames.push(frame);

            tex = cc.textureCache.addImage(anis[0]);
            frame = new cc.SpriteFrame(tex,rect);
            movingFrames.push(frame);

            tex = cc.textureCache.addImage(anis[1]);
            frame = new cc.SpriteFrame(tex,rect);
            movingFrames.push(frame);

            tex = cc.textureCache.addImage(anis[2]);
            frame = new cc.SpriteFrame(tex,rect);
            movingFrames.push(frame);

            tex = cc.textureCache.addImage(anis[2]);
            frame = new cc.SpriteFrame(tex,rect);
            movingFrames.push(frame);

            tex = cc.textureCache.addImage(anis[1]);
            frame = new cc.SpriteFrame(tex,rect);
            movingFrames.push(frame);

        this.movingFrames = movingFrames;
        var dog = new cc.Sprite(this.movingFrames[0]);
        var movingAnimation = new cc.Animation(this.movingFrames, 0.2);
        var movingAction = cc.animate(movingAnimation).repeatForever();
        dog.runAction(movingAction);
        this.addChild(dog,1);
        dog.x = size.width/2;
        dog.y = size.height/2-100;
        dog.scale = 3.5;
    },
    onStartBtn:function(sender){
        cc.isEdit = false;
    	var game = new GameScene();
        cc.director.runScene(game);
        cc.audioEngine.playEffect(res.start);
    },
    onRuleBtn:function(sender){
        cc.audioEngine.playEffect(res.clickcell);
    },
    onEdit:function(){
        cc.isEdit = true;
        var game = new GameScene();
        cc.director.runScene(game);
        cc.audioEngine.playEffect(res.start);
    }
});

//主界面
var HomeScene = cc.Scene.extend({
    onEnter : function () {
        this._super();
        var home = new HomeLayer();
        this.addChild(home);
    }
});